package com.my.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.my.R;
import com.my.adapter.NightSpotAdapter;
import com.my.adapter.NightSpotConfirmAdapter;
import com.my.util.ApiUtil;

import java.util.List;

public class NightSpotConfirmActivity extends AppCompatActivity {

    ListView listView;
    JSONObject user = null;

    Handler confirm = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            JSONObject rest = ((JSONObject) msg.obj);
            Toast.makeText(NightSpotConfirmActivity.this, rest.getStr("msg"), Toast.LENGTH_LONG).show();
            onRestart();
        }
    };
    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            JSONObject rest = ((JSONObject) msg.obj);
            if(rest.getInt("code") == 200) {
                List<JSONObject> list = rest.getJSONObject("data").getJSONArray("list").toList(JSONObject.class);
                listView.setAdapter(new NightSpotConfirmAdapter(NightSpotConfirmActivity.this, R.layout.adapter_night_spot_confirm, list, id -> {
                    new Thread(() -> {
                        Message message = new Message();
                        String body = HttpRequest.post(ApiUtil.HTTP_ADDRESS + "nightSpot/confirm?id=" + id).execute().body();
                        message.obj = new JSONObject(body);
                        confirm.sendMessage(message);
                    }).start();
                }));
            }else {
                Toast.makeText(NightSpotConfirmActivity.this, "服务器异常！请稍后再试！", Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_night_spot_confirm);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        listView = findViewById(R.id.list);
        SharedPreferences preferences = this.getSharedPreferences("LOGIN_DATA", Context.MODE_PRIVATE);
        user = new JSONObject(preferences.getString("user", null));
        new Thread(() -> {
            Message message = new Message();
            String body = HttpRequest.get(ApiUtil.HTTP_ADDRESS + "nightSpot?confirm=false&classId=" + user.getLong("gradeClassId")).execute().body();
            message.obj = new JSONObject(body);
            handler.sendMessage(message);
        }).start();
    }

    @Override
    protected void onRestart() {
        new Thread(() -> {
            Message message = new Message();
            String body = HttpRequest.get(ApiUtil.HTTP_ADDRESS + "nightSpot?confirm=false&classId=" + user.getLong("gradeClassId")).execute().body();
            message.obj = new JSONObject(body);
            handler.sendMessage(message);
        }).start();
        super.onRestart();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 16908332) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}